<template>
  <el-dialog v-loading="loading" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 片段编码 -->
          <el-form-item label="片段编码" prop="snippetCode">
            <!--文本框-->
            <el-input v-model="dataForm.snippetCode"
                      placeholder="片段编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 片段名称 -->
          <el-form-item label="片段名称" prop="snippetName">
            <!--文本框-->
            <el-input v-model="dataForm.snippetName"
                      placeholder="片段名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 片段类型 -->
          <el-form-item label="片段类型" prop="snippetType">
            <!--下拉框-->
            <el-select v-model="dataForm.snippetType" placeholder="片段类型" clearable>
              <el-option v-for="template in $getDictList('GEN_TEMP_APP_TYPE', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 片段内容 -->
          <el-form-item label="片段内容" prop="snippetContent">
            <!--文本域-->
            <el-input type="textarea" :rows="15" v-model="dataForm.snippetContent"
                      placeholder="片段内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 片段描述 -->
          <el-form-item label="片段描述" prop="snippetDesc">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.snippetDesc"
                      placeholder="片段描述"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        snippetCode: '',
        snippetName: '',
        snippetType: '',
        snippetContent: '',
        snippetDesc: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        snippetCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        snippetName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        snippetType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        snippetContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/gen/genCodeSnippet/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/gen/genCodeSnippet/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
